package combat;

import org.bukkit.Bukkit;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import actives.ActiveManager;
import actives.ActiveType;
import main.Plugin;

public class CombatHandler implements Listener {

	public CombatHandler(Plugin plugin) {
		Bukkit.getPluginManager().registerEvents(this, plugin);
	}
	
	@EventHandler
	private void combat(EntityDamageByEntityEvent event) {
		if (event.getEntity() instanceof LivingEntity) {
			if (BleedManager.bleeding()) {
				BleedManager.setBleeding(false);
			}
			else {
				if (event.getDamager() instanceof Player) {
					Player damager = (Player) event.getDamager();
					String weapon = damager.getItemInHand().getType().name();
					if (weapon.contains("_SWORD")) {
						Swords.swordCombat(event);
					}
					else if (weapon.contains("AIR")) {
						Toughness.meleeCombat(event);
					}
				}
				if (event.getDamager() instanceof Arrow) {
					Archery.combat(event);
				}
			}
		}		
	}
	
	@EventHandler
	private void toughness(EntityDamageEvent event) {
		if (event.getEntity() instanceof Player) {
			Toughness.toughness(event);
		}
	}
	
	@EventHandler
	private void combatActives(PlayerInteractEvent event) {
		if (event.getAction().name().contains("RIGHT_CLICK")) {
			Player player = event.getPlayer();
			String weapon = player.getItemInHand().getType().name();
			if (weapon.contains("_SWORD")) {
				ActiveManager.prime(player, ActiveType.SWORDS);
			}
			if (weapon.equals("AIR")) {
				ActiveManager.prime(player, ActiveType.TOUGHNESS);
			}
		}
	}
}
